
import os
import errno
p  =  '/path/to/datafile.dat'

#Let’s consider an example – processing a file. The details of the processing aren’t relevant. All we need to know is that the process_file() function will open a file and read some data from it (possibly failing because it can't find the file).

def process_file(p):
    raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT),p)


#Look before you leap (LBYL)
if os.path.exists(p): 
    process_file(p)
else:
    print('No  such  file  as  {}'.format(p))


#Easier to ask for forgiveness rather than permission 
#(EAFP) - this is the preferred way in Python
try:
    process_file(p)
except OSError as e:
    print('Could  not  process  file  because {}'.format(str(e)))
